var logger = require("pomelo-logger").getLogger(__filename);

module.exports = function (app) {
    return new Remote(app);
};

var Remote = function (app) {
    this.app = app;
};

Remote.prototype.getActivityById = function (msg, callback) {
    let activityService = this.app.get("activityService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    let activity = activityService.getActivityById(parseInt(msg["id"]));
    if(!!activity) {
        callback({
            resultCode: 0,
            message: "ok",
            activity: activity.toJSON()
        });
    }else{
        callback({
            resultCode: 404,
            message: "activity为空.get"
        });
    }
};

Remote.prototype.getAllActivities = function (msg, callback) {
    let activityService = this.app.get("activityService");
    callback({
        resultCode: 0,
        message: "ok",
        activity: activityService.toJSON()
    });
};

Remote.prototype.updateActivityUrl = function (msg, callback) {
    let activityService = this.app.get("activityService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    if(!msg.hasOwnProperty("url")) {
        callback({resultCode: 404, message: "url为空"});
        return;
    }
    activityService.updateActivityUrl(parseInt(msg["id"]), msg["url"], function (activity) {
        if(!!activity) {
            callback({
                resultCode: 0,
                message: "ok",
                activity: activity.toJSON()
            });
        }else{
            callback({
                resultCode: 404,
                message: "activity为空.update"
            });
        }
    });
};

Remote.prototype.updateActivityPriority = function (msg, callback) {
    let activityService = this.app.get("activityService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    if(!msg.hasOwnProperty("priority")) {
        callback({resultCode: 404, message: "url为空"});
        return;
    }
    let priority = isNaN(parseInt(msg.priority)) ? 10 : parseInt(msg.priority);
    activityService.updateActivityPriority(parseInt(msg["id"]), priority, function (activity) {
        if(!!activity) {
            callback({
                resultCode: 0,
                message: "ok",
                activity: activity.toJSON()
            });
        }else{
            callback({
                resultCode: 404,
                message: "activity为空.update"
            });
        }
    });
};

Remote.prototype.updateActivity = function (msg, callback) {
    let activityService = this.app.get("activityService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    if(!msg.hasOwnProperty("title")) {
        callback({resultCode: 404, message: "title为空"});
        return;
    }
    if(!msg.hasOwnProperty("priority")) {
        callback({resultCode: 404, message: "priority为空"});
        return;
    }
    if(!msg.hasOwnProperty("url")) {
        callback({resultCode: 404, message: "url为空"});
        return;
    }
    if(!msg.hasOwnProperty("isHot")) {
        callback({resultCode: 404, message: "isHot为空"});
        return;
    }
    if(!msg.hasOwnProperty("startTime")) {
        callback({resultCode: 404, message: "startTime为空"});
        return;
    }
    if(!msg.hasOwnProperty("endTime")) {
        callback({resultCode: 404, message: "endTime为空"});
        return;
    }
    let id = parseInt(msg.id);
    let title = msg.title;
    let priority = isNaN(parseInt(msg.priority)) ? 10 : parseInt(msg.priority);
    let url = msg.url;
    let isHot = msg.isHot == "true";
    let startTime = parseInt(msg.startTime);
    let endTime = parseInt(msg.endTime);
    activityService.updateActivity(id, title, priority, url, isHot, startTime, endTime, function (activity) {
        logger.info(`ActivityService updateActivity ${JSON.stringify(activity)}`);
        if(!!activity) {
            logger.info(`ActivityService updateActivity ${JSON.stringify(activity)}`);
            callback({
                resultCode: 0,
                message: "ok",
                activity: activity.toJSON()
            });
            return;
        }
        callback({resultCode: 404, message: "activity为空.update"});

    });
};

Remote.prototype.addActivity = function (msg, callback) {
    let activityService = this.app.get("activityService");
    if(!msg.hasOwnProperty("title")) {
        callback({resultCode: 404, message: "title为空"});
        return;
    }
    if(!msg.hasOwnProperty("priority")) {
        callback({resultCode: 404, message: "priority为空"});
        return;
    }
    if(!msg.hasOwnProperty("url")) {
        callback({resultCode: 404, message: "url为空"});
        return;
    }
    if(!msg.hasOwnProperty("isHot")) {
        callback({resultCode: 404, message: "isHot为空"});
        return;
    }
    if(!msg.hasOwnProperty("startTime")) {
        callback({resultCode: 404, message: "startTime为空"});
        return;
    }
    if(!msg.hasOwnProperty("endTime")) {
        callback({resultCode: 404, message: "endTime为空"});
        return;
    }
    let isHot = msg.isHot == "true";
    let priority = isNaN(parseInt(msg.priority)) ? 10 : parseInt(msg.priority);
    let startTime = parseInt(msg.startTime);
    let endTime = parseInt(msg.endTime);
    activityService.addActivity(msg.title, priority, msg.url, isHot, startTime, endTime, function (activity) {
        if(!!activity) {
            callback({
                resultCode: 0,
                message: "ok",
                activity : activity.toJSON()
            });
        }else{
            callback({
                resultCode: 404,
                message: "activity为空.add"
            });
        }
    });
};

Remote.prototype.deleteActivityById = function (msg, callback) {
    let activityService = this.app.get("activityService");
    if(!msg.hasOwnProperty("id")) {
        callback({resultCode: 404, message: "id为空"});
        return;
    }
    activityService.deleteActivityById(parseInt(msg["id"]), function (id) {
        if(!!id) {
            callback({
                resultCode: 0,
                message: "ok",
                activity : {id : id}
            });
        }else{
            callback({
                resultCode: 0,
                message: "activity为空.delete"
            });
        }
    });
};

